package org.example.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.core.utils.PageUtils;
import org.example.product.dao.CategoryDao;
import org.example.product.dao.SkuInfoDao;
import org.example.product.dao.SpuInfoDao;
import org.example.product.entity.SpuInfoEntity;
import org.example.product.service.SpuInfoService;
import org.example.product.to.order.SpuInfoTo;
import org.example.product.vo.CategoryPathVo;
import org.example.product.vo.SpuInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 小杰_cc
 * @version 1.0
 */
@Service("spuInfoService")
public class SpuInfoServiceImpl implements SpuInfoService {

    @Autowired
    private SpuInfoDao spuInfoDao;
    @Autowired
    private CategoryDao categoryDao;

    // 获取spu信息集合
    @Override
    public PageUtils querySpuInfoList(String spuTitle, Long categoryId, Long brandId, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SpuInfoEntity> entities = spuInfoDao.getSpuInfoList(spuTitle, categoryId,brandId,status);
        return new PageUtils(new PageInfo<>(entities));
    }

    // spu基本信息
    @Override
    public SpuInfoVo querySpuInfoById(Long spuId) {
        SpuInfoVo spuInfoVo = spuInfoDao.getSpuInfoById(spuId);
        // 获取到分类完整路径
        List<Long> path = new ArrayList<>();
        path.add(spuInfoVo.getCategoryId());
        List<Long> categoryPath = getCategoryPath(path, spuInfoVo.getCategoryId());
        Collections.reverse(categoryPath);
        spuInfoVo.setCategoryIds(categoryPath);
        return spuInfoVo;
    }

    // 修改spu基本信息
    @Override
    public int editSpuInfo(SpuInfoEntity spuInfo) {
        spuInfo.setUpdateTime(LocalDateTime.now());
        return spuInfoDao.updateSpuInfo(spuInfo);
    }

    // 订单查询spu信息
    @Override
    public Map<Long, SpuInfoTo> queryOrderSpuList(Set<Long> spuIds) {
        List<SpuInfoTo> tos = spuInfoDao.getOrderSpuList(spuIds);
        Map<Long, SpuInfoTo> collect = tos.stream().collect(Collectors.toMap(SpuInfoTo::getSpuId, Function.identity(), (oldValue, newValue) -> newValue));
        return collect;
    }

    @Override
    public boolean isSpuByCategoryIds(List<Long> ids) {
        return spuInfoDao.countSpuByCategoryIds(ids) > 0;
    }

    @Override
    public boolean isSpuByBrandIds(List<Long> ids) {
        return spuInfoDao.countSpuByBrandIds(ids) > 0;
    }

    // 获取分类完整路径方法
    private List<Long> getCategoryPath(List<Long> path, Long id){
        CategoryPathVo categoryIdPath = categoryDao.getCategoryIdPath(id);
        if (!categoryIdPath.getPid().toString().equals("0")){
            path.add(categoryIdPath.getPid());
            getCategoryPath(path, categoryIdPath.getPid());
        }
        return path;
    }
}